package com.zoro.jianyue.ui.base;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.zoro.jianyue.utils.CommonUtils;

import butterknife.ButterKnife;

/**
 * Created by CM-ZR on 2016/1/18.
 */
public abstract class BaseAppCompatActivity extends AppCompatActivity {
    protected static String TAG_LOG = null;
    protected Context mContext = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extars = getIntent().getExtras();
        if (extars != null) {
            getBudleExtras(extars);
        }
        if (setContentViewLayoutId() != 0) {
            setContentView(setContentViewLayoutId());
        } else {
            throw new IllegalArgumentException("You must return a right contentView layout resource Id");
        }

        initViewsAndEvents();
    }


    protected void setSystemBarTintDrawable(Drawable tintDrawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            SystemBarTintManager mTintyManager = new SystemBarTintManager(this);
            if (tintDrawable != null) {
                mTintyManager.setStatusBarTintEnabled(true);
                mTintyManager.setTintDrawable(tintDrawable);
            } else {
                mTintyManager.setStatusBarTintEnabled(false);
                mTintyManager.setTintDrawable(null);
            }
        }

    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
    }

    /**
     * show toast
     *
     * @param msg
     */
    protected void showToast(String msg) {
        //防止遮盖虚拟按键
        if (null != msg && !CommonUtils.isEmpty(msg)) {
            Snackbar.make(getLoadingTargetView(), msg, Snackbar.LENGTH_SHORT).show();
        }
    }

    /**
     * startActivity then finish
     *
     * @param clazz
     */
    protected void readyGoThenKill(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    /**
     * init all views and add events
     */
    protected abstract void initViewsAndEvents();

    /**
     * bind layout resouce file
     *
     * @return id of layout resource
     */
    protected abstract int setContentViewLayoutId();

    /**
     * get bundle data
     *
     * @param extras
     */
    protected abstract void getBudleExtras(Bundle extras);

    /**
     * get loading target view
     *
     * @return
     */
    protected abstract View getLoadingTargetView();
}
